package Visao;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import Controle.AlunoControle;
import Controle.ControleFuncionario;
import Controle.ControleMovimentacao;
import Modelo.Aluno;
import Modelo.Funcionario;
import Modelo.Movimentacao;
import javax.swing.JFormattedTextField;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;

public class Busca extends JFrame {

	private JPanel contentPane;
	private JFormattedTextField textFieldcPF;
	private JTable table;
	private PresencaJTable presenca;
	private JButton btnBuscar;
	private JScrollPane scrollPane;
	private Aluno aluno;
	private AlunoControle controleAluno;
	private Movimentacao movimentacao;
	private ControleMovimentacao controleMovimentacao;
	private ControleDeTelas telas;
	private Funcionario funcionario;
	private ControleFuncionario controleFuncionario;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public Busca() throws ParseException {
		funcionario = new Funcionario();
		controleFuncionario = new ControleFuncionario();
		telas= new ControleDeTelas();
		presenca  = new PresencaJTable();
		aluno = new Aluno();
		controleAluno = new AlunoControle();
		movimentacao = new Movimentacao();
		controleMovimentacao = new ControleMovimentacao();
		setTitle("Busca\r\n");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(null);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnInicio = new JMenu("Inicio");
		mnInicio.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				telas.getInicio().setVisible(true);
				dispose();
			}
		});
		menuBar.add(mnInicio);
		
		JMenu mnCadastro = new JMenu("Cadastro Aluno\r\n");
		mnCadastro.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				telas.getCadastros().setVisible(true);
				dispose();
			}
		});
		menuBar.add(mnCadastro);
		
		JMenu mnCadastroFuncionario = new JMenu("Cadastro Funcionario");
		mnCadastroFuncionario.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				telas.getCadastroFuncionario().setVisible(true);
				dispose();
			}
		});
		menuBar.add(mnCadastroFuncionario);
		
		JMenu mnBuscas = new JMenu("Buscas");
		menuBar.add(mnBuscas);
		
		JMenu mnVisitante = new JMenu("Visitante");
		mnVisitante.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					telas.getVisitante().setVisible(true);
					dispose();
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		menuBar.add(mnVisitante);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		MaskFormatter msk = new MaskFormatter("***.***.***-**");
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(50, 123, 337, 129);
		contentPane.add(scrollPane);
		
		table = new JTable(presenca);
		scrollPane.setViewportView(table);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(0, 139, 139, 50));
		panel.setBounds(50, 10, 334, 102);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblBuscarPresen = new JLabel("Buscar presen\u00E7a dos alunos");
		lblBuscarPresen.setBounds(28, 0, 243, 32);
		panel.add(lblBuscarPresen);
		lblBuscarPresen.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
		
		JLabel lblMatricula = new JLabel("CPF\r\n");
		lblMatricula.setBounds(10, 39, 26, 14);
		panel.add(lblMatricula);
		lblMatricula.setFont(new Font("Tahoma", Font.BOLD, 11));
		
		
		
		textFieldcPF = new JFormattedTextField(msk);
		textFieldcPF.setBounds(38, 36, 109, 20);
		panel.add(textFieldcPF);
		textFieldcPF.setColumns(10);
		
		btnBuscar = new JButton("Buscar");
		btnBuscar.setBounds(168, 35, 91, 23);
		panel.add(btnBuscar);
		
		textField = new JTextField();
		textField.setBounds(37, 71, 222, 20);
		panel.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon("H:\\workspace\\SupportWatchful\\Imagens\\i=magem lois.jpg"));
		lblNewLabel.setBounds(0, 0, 442, 252);
		contentPane.add(lblNewLabel);
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				presenca.limpar();
				try {
					if(controleAluno.buscarPorCpf(textFieldcPF.getText()) != null){
						aluno = controleAluno.buscarPorCpf(textFieldcPF.getText());
						textField.setText(aluno.toString());
						funcionario = null;
					}else{
						funcionario = controleFuncionario.buscarPorCpf(textFieldcPF.getText());
						textField.setText(funcionario.toString());
						aluno = null;
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if(aluno != null){
					presenca.addListaDePresenca(controleMovimentacao.buscarHorarioAluno(aluno.getIdAluno()));
				}
				if(funcionario != null){
					presenca.addListaDePresenca(controleMovimentacao.buscarHorarioFuncionario(funcionario.getIdFuncionario()));
				}
				
				textFieldcPF.setText("");
				
				
			}		
			
		});
	}
}
